package com.hutu.service.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hutu.model.resp.Response;
import com.hutu.model.resp.ResponseVo;
import com.hutu.entity.system.Staff;
import com.hutu.core.enums.system.BusinessStatusEnum;
import com.hutu.mapper.StaffMapper;
import com.hutu.util.JWTUtil;
import com.hutu.util.MD5Util;
import com.hutu.model.dto.vo.other.StaffDeptVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author : admin
 * @Date : 2022/1/30
 */

@Service
public class LoginService extends ServiceImpl<StaffMapper, Staff> {

    @Resource
    private StaffMapper staffMapper;

    public ResponseVo login(Staff staff) {
        String password = MD5Util.MD55(staff.getPassword());
        System.out.println("======> 加密后的密码：" + password);
        LambdaQueryWrapper<Staff> queryWrapper = new LambdaQueryWrapper<Staff>().eq(Staff::getCode, staff.getCode())
                .eq(Staff::getPassword, password);
        Staff selectOne = this.staffMapper.selectOne(queryWrapper);
        if (selectOne != null) {
            // 验证用户状态
            if (selectOne.getStatus() == 1) {
                String token = JWTUtil.generateToken(selectOne.getId(),password);
                return Response.success(selectOne, token); // 返回后台用户信息和token
            }
            return Response.error(BusinessStatusEnum.STAFF_STATUS_ERROR);
        }
        return Response.error("用户名或密码错误！");
    }


    public ResponseVo findByCode(String code) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        List<Staff> staffList = list(wrapper);
        if (staffList.size() > 0) {
            return Response.success();
        }
        return Response.error();
    }
}
